package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.contant.WareContant;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.ware.dao.PurchaseDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.entity.PurchaseEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import com.atguigu.gulimall.ware.service.PurchaseService;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.gulimall.ware.vo.MergeVO;
import com.atguigu.gulimall.ware.vo.PurchaseDoneItemVo;
import com.atguigu.gulimall.ware.vo.PurchaseDoneVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
	@Autowired
	private PurchaseDetailService detailService;

	@Autowired
	private WareSkuService wareSkuService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public PageUtils queryPageUnreceive(Map<String, Object> params) {

		IPage<PurchaseEntity> page = this.page(
				new Query<PurchaseEntity>().getPage(params),
				new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
		);

		return new PageUtils(page);
	}

	@Override
	@Transactional
	public void mergePurchase(MergeVO mergeVO) {
		/*
		*   purchaseId: 1, //整单id
  items:[1,2,3,4] //合并项集
		* */
		Long purchaseId = mergeVO.getPurchaseId();
		// 没有没有采购单id ，则新建采购
		if (purchaseId ==null){
			PurchaseEntity purchaseEntity = new PurchaseEntity();
			purchaseEntity.setStatus(WareContant.PurchaseEnum.CREATED.getCode());
			purchaseEntity.setCreateTime(new Date());
			purchaseEntity.setUpdateTime(new Date());
			this.save(purchaseEntity);
			purchaseId=purchaseEntity.getId();
		}
		List<Long> items = mergeVO.getItems();
		Long finalPurchaseId = purchaseId;
		List<PurchaseDetailEntity> detailEntityList = items.stream().map(i -> {
			PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
			detailEntity.setId(i);
			detailEntity.setPurchaseId(finalPurchaseId);
			detailEntity.setStatus(WareContant.PurchaseDetailEnum.ASSIGNED.getCode());
			return detailEntity;
		}).collect(Collectors.toList());

		detailService.updateBatchById(detailEntityList);

		// 更新 更新时间字段
		PurchaseEntity purchaseEntity = new PurchaseEntity();
		purchaseEntity.setId(purchaseId);
		purchaseEntity.setUpdateTime(new Date());
		this.updateById(purchaseEntity);


	}

	@Override
	public void received(List<Long> ids) {
		//1-确认当前采购单是新建或者是已分配状态
		List<PurchaseEntity> purchaseEntities = ids.stream().map(id -> {
			PurchaseEntity byId = this.getById(id);
			return byId;
		}).filter(item -> {
			if (item.getStatus() == WareContant.PurchaseEnum.CREATED.getCode() ||
					item.getStatus() == WareContant.PurchaseEnum.ASSIGNED.getCode()) {
				return true;
			} else {
				return false;
			}
		}).map(item->{
			item.setStatus(WareContant.PurchaseDetailEnum.RECEIVER.getCode());
			item.setUpdateTime(new Date());
			return item;
		}).collect(Collectors.toList());

		//2-改变采购单的状态
		this.updateBatchById(purchaseEntities);

		// 3- 修改采购项的状态
		purchaseEntities.forEach(item->{
			List<PurchaseDetailEntity> detailEntityList = detailService.listDetailByPurchaseId(item.getId());
			List<PurchaseDetailEntity> collect = detailEntityList.stream().map(e -> {
				PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
				detailEntity.setId(e.getId());
				detailEntity.setStatus(WareContant.PurchaseEnum.PURCHASE.getCode());
				return detailEntity;
			}).collect(Collectors.toList());
			detailService.updateBatchById(collect);
		});
	}

	@Override
	public void done(PurchaseDoneVO doneVO) {
		 Long id = doneVO.getId();
		//2-改变采购项的状态
		Boolean flag = true;
		List<PurchaseDoneItemVo> items = doneVO.getItems();
		ArrayList<PurchaseDetailEntity> updateList = new ArrayList<>();
		for (PurchaseDoneItemVo item : items) {
			PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
			if (item.getStatus() == WareContant.PurchaseDetailEnum.FAILED.getCode()){
				flag =false;
				detailEntity.setStatus(item.getStatus());
			}else {
				detailEntity.setStatus(WareContant.PurchaseDetailEnum.FINISHED.getCode());
				////3、将成功采购的进行入库
				PurchaseDetailEntity entity = detailService.getById(item.getItemId());
				wareSkuService.addStock(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());
			}
			detailEntity.setId(item.getItemId());
			updateList.add(detailEntity);
		}
		detailService.updateBatchById(updateList);


		//1-改变采购单的状态
		PurchaseEntity purchaseEntity = new PurchaseEntity();
		purchaseEntity.setId(id);
		purchaseEntity.setStatus(flag?WareContant.PurchaseDetailEnum.FINISHED.getCode()
				:WareContant.PurchaseDetailEnum.FAILED.getCode());
		purchaseEntity.setUpdateTime(new Date());
		this.updateById(purchaseEntity);


	}

}